const request = require('./request')

/**
 * API接口管理
 */
class API {
  constructor() {
    // baseURL已在request.js中设置为 http://localhost:8080/api
    // 这里不需要重复设置
  }

  // ==================== 用户相关接口 ====================
  
  /**
   * 用户注册
   */
  register(data) {
    return request.post('/auth/register', data)
  }

  /**
   * 用户登录
   */
  login(data) {
    return request.post('/auth/login', data)
  }

  /**
   * 微信登录
   */
  wxLogin(data) {
    return request.post('/auth/wechat-login', data)
  }

  /**
   * 发送验证码
   */
  sendVerifyCode(email) {
    return request.post('/auth/send-code', { email })
  }

  /**
   * 获取用户信息
   */
  getUserInfo() {
    return request.get('/user/info')
  }

  /**
   * 更新用户信息
   */
  updateUserInfo(data) {
    return request.put('/user/info', data)
  }

  // ==================== 店铺相关接口 ====================

  /**
   * 获取附近店铺
   */
  getNearbyStores(latitude, longitude, radius = 10) {
    return request.get('/stores/nearby', {
      latitude,
      longitude,
      radius
    })
  }

  /**
   * 按销量获取店铺
   */
  getStoresBySales(page = 0, size = 10) {
    return request.get('/stores/by-sales', { page, size })
  }

  /**
   * 按评分获取店铺
   */
  getStoresByRating(page = 0, size = 10) {
    return request.get('/stores/by-rating', { page, size })
  }

  /**
   * 搜索店铺
   */
  searchStores(keyword, page = 0, size = 10) {
    return request.get('/stores/search', { keyword, page, size })
  }

  /**
   * 获取店铺详情
   */
  getStoreDetail(storeId) {
    return request.get(`/stores/${storeId}`)
  }

  /**
   * 计算配送信息
   */
  getDeliveryInfo(storeId, latitude, longitude) {
    return request.get(`/stores/${storeId}/delivery-info`, {
      latitude,
      longitude
    })
  }

  // ==================== 商品相关接口 ====================

  /**
   * 获取店铺商品
   */
  getStoreProducts(storeId) {
    return request.get(`/products/store/${storeId}`)
  }

  /**
   * 按分类获取商品
   */
  getProductsByCategory(storeId, categoryId) {
    return request.get(`/products/store/${storeId}/category/${categoryId}`)
  }

  /**
   * 获取推荐商品
   */
  getRecommendedProducts(storeId) {
    return request.get(`/products/store/${storeId}/recommended`)
  }

  /**
   * 搜索商品
   */
  searchProducts(keyword, page = 0, size = 20) {
    return request.get('/products/search', { keyword, page, size })
  }

  /**
   * 获取商品详情
   */
  getProductDetail(productId) {
    return request.get(`/products/${productId}`)
  }

  // ==================== 分类相关接口 ====================

  /**
   * 获取商品分类
   */
  getCategories() {
    return request.get('/categories')
  }

  // ==================== 订单相关接口 ====================

  /**
   * 创建订单
   */
  createOrder(data) {
    return request.post('/orders', data)
  }

  /**
   * 获取用户订单
   */
  getUserOrders(userId, status = null, page = 0, size = 10) {
    const params = { page, size }
    if (status) params.status = status
    return request.get(`/orders/user/${userId}`, params)
  }

  /**
   * 获取订单详情
   */
  getOrderDetail(orderId) {
    return request.get(`/orders/${orderId}`)
  }

  /**
   * 更新订单状态
   */
  updateOrderStatus(orderId, status) {
    return request.put(`/orders/${orderId}/status`, { status })
  }

  /**
   * 取消订单
   */
  cancelOrder(orderId, reason) {
    return request.put(`/orders/${orderId}/cancel`, { 
      reason: reason || '用户取消'
    })
  }

  // ==================== 评论相关接口 ====================

  /**
   * 创建评论
   */
  createReview(data) {
    return request.post('/reviews', data)
  }

  /**
   * 获取店铺评论
   */
  getStoreReviews(storeId, page = 0, size = 10) {
    return request.get(`/reviews/store/${storeId}`, { page, size })
  }

  /**
   * 获取用户评论
   */
  getUserReviews(userId, page = 0, size = 10) {
    return request.get(`/reviews/user/${userId}`, { page, size })
  }

  // ==================== 优惠券相关接口 ====================

  /**
   * 获取可用优惠券
   */
  getAvailableCoupons() {
    return request.get('/coupons/available')
  }

  /**
   * 领取优惠券
   */
  claimCoupon(couponId) {
    return request.post(`/coupons/${couponId}/claim`)
  }

  /**
   * 获取用户优惠券
   */
  getUserCoupons(userId, status = null) {
    const params = {}
    if (status) params.status = status
    return request.get(`/coupons/user/${userId}`, params)
  }

  /**
   * 使用优惠券
   */
  useCoupon(data) {
    return request.post('/coupons/use', data)
  }

  // ==================== 店铺关注相关接口 ====================

  /**
   * 关注店铺
   */
  followStore(storeId) {
    return request.post('/follows', { storeId })
  }

  /**
   * 取消关注
   */
  unfollowStore(storeId) {
    return request.delete(`/follows/${storeId}`)
  }

  /**
   * 获取关注列表
   */
  getFollowedStores(userId) {
    return request.get(`/follows/user/${userId}`)
  }

  /**
   * 检查关注状态
   */
  checkFollowStatus(storeId) {
    return request.get('/follows/check', { storeId })
  }

  // ==================== 地址相关接口 ====================

  /**
   * 获取用户地址列表
   */
  getUserAddresses(userId) {
    return request.get(`/addresses/user/${userId}`)
  }

  /**
   * 添加地址
   */
  addAddress(data) {
    return request.post('/addresses', data)
  }

  /**
   * 更新地址
   */
  updateAddress(addressId, data) {
    return request.put(`/addresses/${addressId}`, data)
  }

  /**
   * 删除地址
   */
  deleteAddress(addressId) {
    return request.delete(`/addresses/${addressId}`)
  }

  // ==================== 文件上传接口 ====================

  /**
   * 上传图片
   */
  uploadImage(filePath, type = 'review') {
    return request.upload('/upload/image', filePath, {
      name: 'file',
      formData: { type }
    })
  }

  /**
   * 上传头像
   */
  uploadAvatar(filePath) {
    return request.upload('/upload/avatar', filePath, {
      name: 'avatar'
    })
  }

  // ==================== 支付相关接口 ====================

  /**
   * 创建支付订单
   */
  createPayment(orderId, paymentMethod) {
    return request.post('/payment/create', {
      orderId,
      paymentMethod
    })
  }

  /**
   * 查询支付状态
   */
  getPaymentStatus(paymentId) {
    return request.get(`/payment/status/${paymentId}`)
  }

  // ==================== 统计相关接口 ====================

  /**
   * 获取系统统计数据
   */
  getSystemStats() {
    return request.get('/admin/stats')
  }

  /**
   * 导出数据
   */
  exportData(type) {
    return request.get(`/admin/export/${type}`)
  }
}

// 创建API实例
const api = new API()

module.exports = api 